package com.example.multilevelapprovalback.service.impl;

import com.example.multilevelapprovalback.mapper.TeachProjMapper;
import com.example.multilevelapprovalback.pojo.PageBean;
import com.example.multilevelapprovalback.pojo.TeachingProject;
import com.example.multilevelapprovalback.service.TeachProjService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.Year;
import java.util.ArrayList;
import java.util.List;
@Service
public class TeachProjServiceImpl implements TeachProjService {
    @Autowired
    private TeachProjMapper teachProjMapper;
    /**
     * 添加教学项目
     */
    @Override
    public void save(TeachingProject teachingProject) {
        String codeNumber = teachingProject.getJobNumber() + System.currentTimeMillis();
        teachingProject.setCodeNumber(codeNumber);
        teachingProject.setAchievementTypeId(1);  //类别设置为教学项目类
        teachingProject.setStatusId(1);  //状态设置为待提交
        teachingProject.setCreateTime(LocalDateTime.now());  //给项目创建时间赋为当前提交时间
        teachingProject.setUpdateTime(LocalDateTime.now());  //给项目更新时间赋值为当前提交时间
        teachingProject.setMark(0);  //项目标记位设置为0
        teachProjMapper.add(teachingProject);
    }
    /**
     * 更新教学项目
     * @param teachingProject
     */
    @Override
    public void update(TeachingProject teachingProject) {
        if(teachingProject.getMark() == null) {                  //如果不是修改标记位
            teachingProject.setUpdateTime(LocalDateTime.now());  //给项目更新时间赋值为当前提交时间
        }
        teachProjMapper.update(teachingProject);
    }

    /**
     * 批量删除教学项目
     * @param ids
     */
    @Override
    public void delete(List<String> ids) {
        teachProjMapper.delete(ids);
    }

    /**
     * 查询教学项目
     * @param page
     * @param pageSize
     * @param name
     * @param jobNumber
     * @param startTime
     * @param endTime
     * @param statusId
     * @param projectName
     * @return
     */
    @Override
    public PageBean page(Integer page, Integer pageSize, Integer roleId, String name, String jobNumber, String  startTime,
                         String endTime, Integer statusId, String projectName, Integer mark, String checkFilterValues,
                         String levelFilterValues, Integer num) {
        List<Integer> levelIds = new ArrayList<>();
        //需要显示的级别非空时，将其转换成整数数组
        if(levelFilterValues != null && levelFilterValues != "") {
            String[] strArray = levelFilterValues.split(",");
            for(int i = 0; i < strArray.length; i++) {
                levelIds.add(Integer.parseInt(strArray[i]));
            }
        }
        List<Integer> statusIds = new ArrayList<>();
        //需要显示的状态非空时,将其转换成整数数组
        if(checkFilterValues != null && checkFilterValues != "") {
            String[] strArray = checkFilterValues.split(",");
            for(int i = 0; i < strArray.length; i++) {
                statusIds.add(Integer.parseInt(strArray[i]));
            }
        }
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);
        //2.执行查询
        List<TeachingProject> teachingProjectList = new ArrayList<TeachingProject>();
        if(roleId == 1 || (roleId == 3 && statusId == null)) {  //教师查询 和 副院长查询
            if(projectName == null && startTime == null && endTime==null) {   //搜索值为空时的查询
                if(mark == null) {  //非项目标记数查询
                    if((checkFilterValues != null && checkFilterValues != "") || (levelFilterValues != null && levelFilterValues != "")) {   //指定项目状态或级别的查询
                        teachingProjectList = teachProjMapper.teacherListBystatusIds(jobNumber,statusId,statusIds,levelIds);
                    } else {   //非指定项目状态和级别的查询
                        teachingProjectList = teachProjMapper.teacherList(jobNumber, statusId);
                    }
                } else {   //项目标记数查询，用于菜单栏的标记显示
                    teachingProjectList = teachProjMapper.markList(jobNumber, mark);
                }
            } else {   //搜索值非空时的查询
                if((checkFilterValues != null && checkFilterValues != "") || (levelFilterValues != null && levelFilterValues != "")) {    //指定项目状态或级别的查询
                    teachingProjectList = teachProjMapper.teacherSearchListBystatusIds(jobNumber, startTime,endTime, statusId, projectName,statusIds,levelIds);
                } else {   //非指定项目状态和级别的查询
                    teachingProjectList = teachProjMapper.teacherSearchList(jobNumber, startTime,endTime, statusId, projectName);
                }
            }
        } else if(roleId==2 && statusId!=4){
            //教秘返回>=2的项目  返回的statusId=2
            if(num != null) {
                teachingProjectList = teachProjMapper.checkNum(statusId);
            } else {
                teachingProjectList = teachProjMapper.teacherSearchAllList(name,jobNumber, startTime,endTime, statusId, projectName);
            }
        } else if (roleId==3 && statusId!=4) {
            //副院长返回>=3的项目  返回的statusId=3
            if(num != null) {
                teachingProjectList = teachProjMapper.checkNum(statusId);
            } else {
                teachingProjectList = teachProjMapper.teacherSearchAllList(name,jobNumber, startTime,endTime, statusId, projectName);
            }
        } else {
            teachingProjectList = teachProjMapper.list(name, jobNumber, startTime,endTime, statusId, projectName);
        }

        Page<TeachingProject> p =(Page<TeachingProject>) teachingProjectList;

        //3.封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }
}
